package cn.org.eshow.service.impl;

import cn.org.eshow.bean.query.WithdrawAccountQuery;
import cn.org.eshow.common.page.Page;
import cn.org.eshow.dao.WithdrawAccountDao;
import cn.org.eshow.model.User;
import cn.org.eshow.model.WithdrawAccount;
import cn.org.eshow.service.WithdrawAccountManager;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.jws.WebService;
import java.util.Date;
import java.util.List;

@WebService(serviceName = "WithdrawAccountService", endpointInterface = "cn.org.eshow.service.WithdrawAccountManager")
@Service
public class WithdrawAccountManagerImpl extends GenericManagerImpl<WithdrawAccount, Integer> implements WithdrawAccountManager {

    @Autowired
    WithdrawAccountDao withdrawAccountDao;

    public WithdrawAccountManagerImpl() {
    }

    @Autowired
    public WithdrawAccountManagerImpl(WithdrawAccountDao withdrawAccountDao) {
        super(withdrawAccountDao);
        this.withdrawAccountDao = withdrawAccountDao;
    }

    @Override
    public List<WithdrawAccount> list(WithdrawAccountQuery query) {
        return withdrawAccountDao.list(query);
    }

    @Override
    public Page<WithdrawAccount> search(WithdrawAccountQuery query) {
        return withdrawAccountDao.search(query);
    }

    @Override
    public WithdrawAccount mine(User user) {
        List<WithdrawAccount> withdrawAccounts = withdrawAccountDao.list(new WithdrawAccountQuery(user.getId()));
        if (withdrawAccounts.isEmpty()) {
            WithdrawAccount withdrawAccount = new WithdrawAccount();
            withdrawAccount.setUser(user);
            return withdrawAccountDao.save(withdrawAccount);
        }
        return withdrawAccounts.get(0);
    }

    @Override
    public WithdrawAccount update(WithdrawAccount old, WithdrawAccount withdrawAccount) {
        old.setUpdateTime(new Date());
        //更新银行卡信息
        if (StringUtils.isNotEmpty(withdrawAccount.getBankName()) && StringUtils.isNotEmpty(withdrawAccount.getBankCardNo()) && StringUtils.isNotEmpty(withdrawAccount.getBankAccountName())) {
            old.setBankName(StringUtils.isEmpty(withdrawAccount.getBankName()) ? old.getBankName() : withdrawAccount.getBankName());
            old.setBankCardNo(StringUtils.isEmpty(withdrawAccount.getBankCardNo()) ? old.getBankCardNo() : withdrawAccount.getBankCardNo());
            old.setBankAccountName(StringUtils.isEmpty(withdrawAccount.getBankAccountName()) ? old.getBankAccountName() : withdrawAccount.getBankAccountName());
        }
        //更新支付宝信息
        if (StringUtils.isNotEmpty(withdrawAccount.getAlipayName()) && StringUtils.isNotEmpty(withdrawAccount.getAlipayAccount())) {
            old.setAlipayName(StringUtils.isEmpty(withdrawAccount.getAlipayName()) ? old.getAlipayName() : withdrawAccount.getAlipayName());
            old.setAlipayAccount(StringUtils.isEmpty(withdrawAccount.getAlipayAccount()) ? old.getAlipayAccount() : withdrawAccount.getAlipayAccount());
        }
        //更新微信信息
        if (StringUtils.isNotEmpty(withdrawAccount.getWechatOpenID()) && StringUtils.isNotEmpty(withdrawAccount.getWechatNickname())) {
            old.setWechatOpenID(StringUtils.isEmpty(withdrawAccount.getWechatOpenID()) ? old.getWechatOpenID() : withdrawAccount.getWechatOpenID());
            old.setWechatUnionID(StringUtils.isEmpty(withdrawAccount.getWechatUnionID()) ? old.getWechatUnionID() : withdrawAccount.getWechatUnionID());
            old.setWechatNickname(StringUtils.isEmpty(withdrawAccount.getWechatNickname()) ? old.getWechatNickname() : withdrawAccount.getWechatNickname());
        }
        return withdrawAccountDao.save(old);
    }

    @Override
    public WithdrawAccount check(WithdrawAccountQuery query) {
        List<WithdrawAccount> list = withdrawAccountDao.list(query);
        if (!list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public Long count(WithdrawAccountQuery query) {
        return withdrawAccountDao.count(query);
    }

}